{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Chapter 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Assigning loaded ​weights - Single weight array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Saving the weights of the trained model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "DATA_DIR = '/tmp/data'\n",
    "NUM_STEPS = 1000\n",
    "MINIBATCH_SIZE = 100\n",
    "\n",
    "\n",
    "data = input_data.read_data_sets(DATA_DIR, one_hot=True)\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "W = tf.Variable(tf.zeros([784, 10]))\n",
    "\n",
    "y_true = tf.placeholder(tf.float32, [None, 10])\n",
    "y_pred = tf.matmul(x, W)\n",
    "\n",
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(\n",
    "    logits=y_pred, labels=y_true))\n",
    "\n",
    "\n",
    "gd_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "\n",
    "correct_mask = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "\n",
    "    # Train\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    for _ in range(NUM_STEPS):\n",
    "        batch_xs, batch_ys = data.train.next_batch(MINIBATCH_SIZE)\n",
    "        sess.run(gd_step, feed_dict={x: batch_xs, y_true: batch_ys})\n",
    "\n",
    "    weights = sess.run(W)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os \n",
    "path = 'tmp//'\n",
    "\n",
    "np.savez(os.path.join(path, 'weight_storage'), weights)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "loaded_w = np.load(path + 'weight_storage.npz')\n",
    "loaded_w = loaded_w.items()[0][1]\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "W = tf.Variable(tf.zeros([784, 10]))\n",
    "y_true = tf.placeholder(tf.float32, [None, 10])\n",
    "y_pred = tf.matmul(x, W)\n",
    "cross_entropy = tf.reduce_mean(\n",
    "             tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true))\n",
    "gd_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "correct_mask = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    # assigning loaded weights\n",
    "    sess.run(W.assign(loaded_w))\n",
    "    acc = sess.run(accuracy, feed_dict={x: data.test.images, \n",
    "                                        y_true: data.test.labels})\n",
    "\n",
    "print(\"Accuracy: {}\".format(acc))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Saving multiple weight arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class simple_cnn:\n",
    "    def __init__(self, x_image,keep_prob, weights=None, sess=None):\n",
    "        \n",
    "        self.parameters = []\n",
    "        self.x_image = x_image\n",
    "\n",
    "        conv1 = self.conv_layer(x_image, shape=[5, 5, 1, 32])\n",
    "        conv1_pool = self.max_pool_2x2(conv1)\n",
    "\n",
    "        conv2 = self.conv_layer(conv1_pool, shape=[5, 5, 32, 64])\n",
    "        conv2_pool = self.max_pool_2x2(conv2)\n",
    "\n",
    "        conv2_flat = tf.reshape(conv2_pool, [-1, 7*7*64])\n",
    "        full_1 = tf.nn.relu(self.full_layer(conv2_flat, 1024))\n",
    "\n",
    "        full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob)\n",
    "\n",
    "        self.y_conv = self.full_layer(full1_drop, 10)\n",
    "        \n",
    "        if weights is not None and sess is not None:\n",
    "            self.load_weights(weights, sess)\n",
    "            \n",
    "    def weight_variable(self,shape):\n",
    "        initial = tf.truncated_normal(shape, stddev=0.1)\n",
    "        return tf.Variable(initial,name='weights')\n",
    "\n",
    "\n",
    "    def bias_variable(self,shape):\n",
    "        initial = tf.constant(0.1, shape=shape)\n",
    "        return tf.Variable(initial,name='biases')\n",
    "\n",
    "\n",
    "    def conv2d(self,x, W):\n",
    "        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], \n",
    "                                       padding='SAME')\n",
    "\n",
    "\n",
    "    def max_pool_2x2(self,x):\n",
    "        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],\n",
    "                              strides=[1, 2, 2, 1], padding='SAME')\n",
    "\n",
    "\n",
    "    def conv_layer(self,input, shape):\n",
    "        W = self.weight_variable(shape)\n",
    "        b = self.bias_variable([shape[3]])\n",
    "        self.parameters += [W, b]\n",
    "\n",
    "        return tf.nn.relu(self.conv2d(input, W) + b)\n",
    "\n",
    "\n",
    "    def full_layer(self,input, size):\n",
    "        in_size = int(input.get_shape()[1])\n",
    "        W = self.weight_variable([in_size, size])\n",
    "        b = self.bias_variable([size])\n",
    "        self.parameters += [W, b]\n",
    "        return tf.matmul(input, W) + b\n",
    "    \n",
    "\n",
    "    def load_weights(self, weights, sess):\n",
    "        for i,w in enumerate(weights):\n",
    "            print(\"Weight index: {}\".format(i), \n",
    "                               \"Weight shape: {}\".format(w.shape))\n",
    "            sess.run(self.parameters[i].assign(w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "NUM_STEPS = 5000\n",
    "\n",
    "x = tf.placeholder(tf.float32, shape=[None, 784])\n",
    "x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "y_ = tf.placeholder(tf.float32, shape=[None, 10])\n",
    "keep_prob = tf.placeholder(tf.float32)\n",
    "\n",
    "sess = tf.Session()\n",
    "\n",
    "cnn = simple_cnn(x_image,keep_prob, sess)\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "            tf.nn.softmax_cross_entropy_with_logits(\n",
    "                                                 logits=cnn.y_conv, labels= y_))\n",
    "train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n",
    "\n",
    "correct_prediction = tf.equal(tf.argmax(cnn.y_conv, 1), \n",
    "                                      tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "sess.run(tf.global_variables_initializer())\n",
    "X = data.test.images.reshape(10, 1000, 784)\n",
    "Y = data.test.labels.reshape(10, 1000, 10)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "for _ in range(NUM_STEPS):\n",
    "    batch_xs, batch_ys = data.train.next_batch(MINIBATCH_SIZE)\n",
    "    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys ,keep_prob:1.0})\n",
    "\n",
    "test_accuracy = np.mean([sess.run(accuracy, \n",
    "                         feed_dict={x:X[i], y_:Y[i],keep_prob:1.0}) \n",
    "                         for i in range(10)])    \n",
    "\n",
    "\n",
    "\n",
    "path = 'tmp//'\n",
    "weights = sess.run(cnn.parameters)\n",
    "np.savez(os.path.join(path, 'cnn_weight_storage'), weights)\n",
    "\n",
    "sess.close()\n",
    "\n",
    "print(\"test accuracy: {}\".format(test_accuracy))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loading weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "x = tf.placeholder(tf.float32, shape=[None, 784])\n",
    "x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "y_ = tf.placeholder(tf.float32, shape=[None, 10])\n",
    "keep_prob = tf.placeholder(tf.float32)\n",
    "\n",
    "sess = tf.Session()\n",
    "\n",
    "weights = np.load(path +'cnn_weight_storage.npz')\n",
    "weights = weights.items()[0][1]\n",
    "cnn = simple_cnn(x_image,keep_prob,weights, sess)\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "            tf.nn.softmax_cross_entropy_with_logits(\n",
    "                                                 logits=cnn.y_conv, labels= y_))\n",
    "train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n",
    "\n",
    "correct_prediction = tf.equal(tf.argmax(cnn.y_conv, 1), \n",
    "                                      tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "X = data.test.images.reshape(10, 1000, 784)\n",
    "Y = data.test.labels.reshape(10, 1000, 10)\n",
    "test_accuracy = np.mean([sess.run(accuracy, \n",
    "                         feed_dict={x:X[i], y_:Y[i],keep_prob:1.0}) \n",
    "                         for i in range(10)])\n",
    "\n",
    "sess.close()\n",
    "\n",
    "print(\"test accuracy: {}\".format(test_accuracy))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Saver class - save and restore weights only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "DATA_DIR = '/tmp/data'\n",
    "data = input_data.read_data_sets(DATA_DIR, one_hot=True)\n",
    "\n",
    "NUM_STEPS = 1000\n",
    "MINIBATCH_SIZE = 100\n",
    "\n",
    "DIR = 'saved_model/'\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784],name='x')\n",
    "W = tf.Variable(tf.zeros([784, 10]),name='W')\n",
    "y_true = tf.placeholder(tf.float32, [None, 10])\n",
    "y_pred = tf.matmul(x, W)\n",
    "cross_entropy = tf.reduce_mean(\n",
    "             tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true))\n",
    "gd_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "correct_mask = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))\n",
    "\n",
    "saver = tf.train.Saver(max_to_keep=7, \n",
    "                       keep_checkpoint_every_n_hours=1)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(1,NUM_STEPS+1):\n",
    "        batch_xs, batch_ys = data.train.next_batch(MINIBATCH_SIZE)\n",
    "        sess.run(gd_step, feed_dict={x: batch_xs, y_true: batch_ys})\n",
    "        \n",
    "        if step % 50 == 0:\n",
    "            saver.save(sess, os.path.join(DIR, \"model_ckpt\"), \n",
    "                                           global_step=step)\n",
    "    \n",
    "    ans = sess.run(accuracy, feed_dict={x: data.test.images, \n",
    "                                        y_true: data.test.labels})\n",
    "\n",
    "print(\"Accuracy: {:.4}%\".format(ans*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph() \n",
    "x = tf.placeholder(tf.float32, [None, 784],name='x')\n",
    "W = tf.Variable(tf.zeros([784, 10]),name='W')\n",
    "y_true = tf.placeholder(tf.float32, [None, 10])\n",
    "y_pred = tf.matmul(x, W)\n",
    "cross_entropy = tf.reduce_mean(\n",
    "             tf.nn.softmax_cross_entropy_with_logits(logits=y_pred,labels=y_true))\n",
    "gd_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "correct_mask = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))\n",
    "\n",
    "saver = tf.train.Saver()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    saver.restore(sess, os.path.join(DIR,\"model_ckpt-1000\"))\n",
    "    ans = sess.run(accuracy, feed_dict={x: data.test.images, \n",
    "                                        y_true: data.test.labels})\n",
    "\n",
    "print(\"Accuracy: {:.4}%\".format(ans*100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save and restore weights + graph + collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "DATA_DIR = '/tmp/data'\n",
    "data = input_data.read_data_sets(DATA_DIR, one_hot=True)\n",
    "\n",
    "NUM_STEPS = 1000\n",
    "MINIBATCH_SIZE = 100\n",
    "\n",
    "DIR = 'saved_model/'\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784],name='x')\n",
    "W = tf.Variable(tf.zeros([784, 10]),name='W')\n",
    "y_true = tf.placeholder(tf.float32, [None, 10])\n",
    "y_pred = tf.matmul(x, W)\n",
    "cross_entropy = tf.reduce_mean(\n",
    "             tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true))\n",
    "gd_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "correct_mask = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))\n",
    "\n",
    "saver = tf.train.Saver(max_to_keep=7, \n",
    "                       keep_checkpoint_every_n_hours=1)\n",
    "\n",
    "train_var = [x,y_true,accuracy]\n",
    "tf.add_to_collection('train_var', train_var[0])\n",
    "tf.add_to_collection('train_var', train_var[1])\n",
    "tf.add_to_collection('train_var', train_var[2])\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(1,NUM_STEPS+1):\n",
    "        batch_xs, batch_ys = data.train.next_batch(MINIBATCH_SIZE)\n",
    "        sess.run(gd_step, feed_dict={x: batch_xs, y_true: batch_ys})\n",
    "        \n",
    "        if step % 50 == 0:\n",
    "            saver.export_meta_graph(os.path.join(DIR,\"model_ckpt.meta\"),collection_list=['train_var'])\n",
    "            saver.save(sess, os.path.join(DIR, \"model_ckpt\"), \n",
    "                                           global_step=step)\n",
    "    \n",
    "    ans = sess.run(accuracy, feed_dict={x: data.test.images, \n",
    "                                        y_true: data.test.labels})\n",
    "\n",
    "print(\"Accuracy: {:.4}%\".format(ans*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph() \n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    saver = tf.train.import_meta_graph(os.path.join(DIR,\"model_ckpt.meta\"))\n",
    "    saver.restore(sess,  os.path.join(DIR,\"model_ckpt-1000\"))\n",
    "    x =  tf.get_collection('train_var')[0]\n",
    "    y_true =  tf.get_collection('train_var')[1]\n",
    "    accuracy =  tf.get_collection('train_var')[2]\n",
    "\n",
    "    ans = sess.run(accuracy, feed_dict={x: data.test.images, \n",
    "                                        y_true: data.test.labels})\n",
    "print(\"Accuracy: {:.4}%\".format(ans*100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### TensorFlow Serving"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import tensorflow as tf\n",
    "from tensorflow.python.saved_model import builder \n",
    "                                           as saved_model_builder\n",
    "from tensorflow.python.saved_model import signature_constants\n",
    "from tensorflow.python.saved_model import signature_def_utils\n",
    "from tensorflow.python.saved_model import tag_constants\n",
    "from tensorflow.python.saved_model import utils\n",
    "from tensorflow.python.util import compat\n",
    "from tensorflow_serving.example import mnist_input_data\n",
    "\n",
    "tf.app.flags.DEFINE_integer('training_iteration', 10,\n",
    "                            'number of training iterations.')\n",
    "tf.app.flags.DEFINE_integer(\n",
    "                 'model_version', 1, 'version number of the model.')\n",
    "tf.app.flags.DEFINE_string('work_dir', '/tmp', 'Working directory.')\n",
    "FLAGS = tf.app.flags.FLAGS\n",
    "\n",
    "\n",
    "def weight_variable(shape):\n",
    "  initial = tf.truncated_normal(shape, stddev=0.1)\n",
    "  return tf.Variable(initial,dtype='float')\n",
    "\n",
    "def bias_variable(shape):\n",
    "  initial = tf.constant(0.1, shape=shape)\n",
    "  return tf.Variable(initial,dtype='float')\n",
    "\n",
    "def conv2d(x, W):\n",
    "  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\n",
    "\n",
    "def max_pool_2x2(x):\n",
    "  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='SAME')\n",
    "\n",
    "def main(_):\n",
    "    if len(sys.argv) < 2 or sys.argv[-1].startswith('-'):\n",
    "        print('Usage: mnist_export.py [--training_iteration=x] '\n",
    "              '[--model_version=y] export_dir')\n",
    "        sys.exit(-1)\n",
    "    if FLAGS.training_iteration <= 0:\n",
    "        print('Please specify a positive \n",
    "                                 value for training iteration.')\n",
    "        sys.exit(-1)\n",
    "    if FLAGS.model_version <= 0:\n",
    "        print ('Please specify a positive \n",
    "                                     value for version number.')\n",
    "        sys.exit(-1)\n",
    "    \n",
    "\n",
    "    print('Training...')\n",
    "    mnist = mnist_input_data.read_data_sets(\n",
    "                                  FLAGS.work_dir, one_hot=True)\n",
    "    sess = tf.InteractiveSession()\n",
    "    serialized_tf_example = tf.placeholder(\n",
    "                                  tf.string, name='tf_example')\n",
    "    feature_configs = {'x': tf.FixedLenFeature(shape=[784],\\ \n",
    "                                            dtype=tf.float32),}\n",
    "    tf_example = tf.parse_example(serialized_tf_example, \n",
    "                                                feature_configs)\n",
    "    \n",
    "    \n",
    "    x = tf.identity(tf_example['x'], name='x')  \n",
    "    y_ = tf.placeholder('float', shape=[None, 10])\n",
    "\n",
    "    W_conv1 = weight_variable([5, 5, 1, 32])\n",
    "    b_conv1 = bias_variable([32])\n",
    "    x_image = tf.reshape(x, [-1,28,28,1])\n",
    "\n",
    "    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\n",
    "    h_pool1 = max_pool_2x2(h_conv1)\n",
    "\n",
    "    W_conv2 = weight_variable([5, 5, 32, 64])\n",
    "    b_conv2 = bias_variable([64])\n",
    "\n",
    "    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)\n",
    "    h_pool2 = max_pool_2x2(h_conv2)\n",
    "\n",
    "\n",
    "    W_fc1 = weight_variable([7 * 7 * 64, 1024])\n",
    "    b_fc1 = bias_variable([1024])\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n",
    "\n",
    "\n",
    "    keep_prob = tf.placeholder(tf.float32)\n",
    "    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n",
    "\n",
    "    W_fc2 = weight_variable([1024, 10])\n",
    "    b_fc2 = bias_variable([10])\n",
    "\n",
    "    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2\n",
    "    \n",
    " \n",
    "    y = tf.nn.softmax(y_conv, name='y')\n",
    "    cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))\n",
    "    train_step = tf.train.AdamOptimizer(1e-4).\\\n",
    "                                          minimize(cross_entropy)\n",
    "   \n",
    "    \n",
    "    values, indices = tf.nn.top_k(y_conv, 10)\n",
    "    prediction_classes = tf.contrib.lookup.index_to_string(\n",
    "      tf.to_int64(indices), \n",
    "      mapping=tf.constant([str(i) for i in xrange(10)]))\n",
    "    \n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    for _ in range(FLAGS.training_iteration):\n",
    "        batch = mnist.train.next_batch(50)\n",
    "        \n",
    "        train_step.run(feed_dict={x: batch[0], \n",
    "                                 y_: batch[1], keep_prob: 0.5})\n",
    "        print(_)\n",
    "        correct_prediction = tf.equal(tf.argmax(y_conv,1), \n",
    "                                         tf.argmax(y_,1))\n",
    "\n",
    "    \n",
    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))\n",
    "                       y_: mnist.test.labels})\n",
    "    \n",
    "    print('training accuracy %g' % accuracy.eval(feed_dict={\n",
    "        x: mnist.test.images, \n",
    "        y_: mnist.test.labels, keep_prob: 1.0}))\n",
    "\n",
    "    print('training is finished!')\n",
    "    \n",
    "    export_path_base = sys.argv[-1]\n",
    "    export_path = os.path.join(\n",
    "      compat.as_bytes(export_path_base),\n",
    "      compat.as_bytes(str(FLAGS.model_version)))\n",
    "    print 'Exporting trained model to', export_path\n",
    "    builder = saved_model_builder.SavedModelBuilder(export_path)\n",
    "\n",
    "    classification_inputs = utils.build_tensor_info(\n",
    "                                             serialized_tf_example)\n",
    "    classification_outputs_classes = utils.build_tensor_info(\n",
    "                                             prediction_classes)\n",
    "    classification_outputs_scores = utils.build_tensor_info(values)\n",
    "\n",
    "    classification_signature = signature_def_utils.\\\n",
    "      build_signature_def(\n",
    "      inputs={signature_constants.CLASSIFY_INPUTS:\\ \n",
    "                           classification_inputs},\n",
    "      outputs={\n",
    "          signature_constants.CLASSIFY_OUTPUT_CLASSES:\n",
    "              classification_outputs_classes,\n",
    "          signature_constants.CLASSIFY_OUTPUT_SCORES:\n",
    "              classification_outputs_scores\n",
    "      },\n",
    "      method_name=signature_constants.CLASSIFY_METHOD_NAME)\n",
    "\n",
    "    tensor_info_x = utils.build_tensor_info(x)\n",
    "    tensor_info_y = utils.build_tensor_info(y_conv)\n",
    "\n",
    "    prediction_signature = signature_def_utils.build_signature_def(\n",
    "      inputs={'images': tensor_info_x},\n",
    "      outputs={'scores': tensor_info_y},\n",
    "      method_name=signature_constants.PREDICT_METHOD_NAME)\n",
    "\n",
    "    legacy_init_op = tf.group(tf.initialize_all_tables(), \n",
    "                                   name='legacy_init_op')\n",
    "    builder.add_meta_graph_and_variables(\n",
    "      sess, [tag_constants.SERVING],\n",
    "      signature_def_map={\n",
    "          'predict_images':\n",
    "              prediction_signature,\n",
    "          signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:\n",
    "              classification_signature,\n",
    "      },\n",
    "      legacy_init_op=legacy_init_op)\n",
    "\n",
    "    builder.save()\n",
    "\n",
    "    print('new model exported!')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    tf.app.run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf_1_1",
   "language": "python",
   "name": "tf_1_1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
